/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pku.lbs.web;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

/**
 *
 * @author yongchao
 */
public class SmsWeb {

    static private SmsWeb instance = null;

    private SmsWeb() {}

    static public SmsWeb getInstance() {
        if (instance == null) {
            instance = new SmsWeb();
        }
        return instance;
    }

    private void doIt(String tel, String sms) throws Exception {
		Service srv;
		OutboundMessage msg;
		OutboundNotification outboundNotification = new OutboundNotification();
		System.out.println("Example: Send message from a serial gsm modem.");
		System.out.println(Library.getLibraryDescription());
		System.out.println("Version: " + Library.getLibraryVersion());
		srv = new Service();
		SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM4", 9600, "Nokia", "n78");
		gateway.setInbound(true);
		gateway.setOutbound(true);
		gateway.setSimPin("0000");
		srv.setOutboundNotification(outboundNotification);
		srv.addGateway(gateway);
		srv.startService();
		System.out.println();
		System.out.println("Modem Information:");
		System.out.println("  Manufacturer: " + gateway.getManufacturer());
		System.out.println("  Model: " + gateway.getModel());
		System.out.println("  Serial No: " + gateway.getSerialNo());
		System.out.println("  SIM IMSI: " + gateway.getImsi());
		System.out.println("  Signal Level: " + gateway.getSignalLevel() + "%");
		System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
		System.out.println();
		// Send a message synchronously.
		msg = new OutboundMessage(tel, sms);
		srv.sendMessage(msg);
		System.out.println(msg);
		srv.stopService();
	}

	private class OutboundNotification implements IOutboundMessageNotification {
		public void process(String gatewayId, OutboundMessage msg) {
			System.out.println("Outbound handler called from Gateway: " + gatewayId);
			System.out.println(msg);
		}
	}

    public String doSms(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String posX = request.getParameter("pos_x");
        String posY = request.getParameter("pos_y");
        String tel = request.getParameter("tel");
        String sms = request.getParameter("sms");

        JSONObject json = new JSONObject();
        try {
            doIt(tel, sms);
            json.put("RETURN", true);
        } catch (Exception ex) {
            json.put("RETURN", false);
            json.put("MESSAGE", ex.getCause());
        }

        return json.toString();
    }
}